package com.icangya.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icangya.DAO.IAdminDAO;
import com.icangya.DAO.IAgentImageDAO;
import com.icangya.DAO.IUserFootDAO;
import com.icangya.domain.Admin;
import com.icangya.domain.AgentImage;
import com.icangya.domain.User;
import com.icangya.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service("AgentServiceImpl")
public class AgentServiceImpl implements IAgentService{

    @Autowired
    private IAdminDAO iAdminDAO;

    @Autowired
    private IUserFootDAO iUserFootDAO;

    @Autowired
    private IAgentImageDAO iAgentImageDAO;

    @Override
    public Admin addAgentUser(Admin admin) {

        //添加用户成功之后
        //修改用户proxy_id
        Admin admin1=iAdminDAO.get(iAdminDAO.save(admin));

        iUserFootDAO.updateProxyId(admin1.getUid(),null);
//        // 二、更新：大于等于新增节点的左下标值的右下标值都加2
//        iAgentService.updateAgentRightNum(use.getLeftNum(),use.getId());
//        //三、更新：大于等于新增节点的右下标值的左下标值都加2
//        iAgentService.updateAgentLeftNum(use.getRightNum(),use.getId());
        return admin1;


    }

    @Override
    public AgentImage addAgentImage(AgentImage agentImage) {

        AgentImage agentImg=   iAgentImageDAO.getAgentImageByAgentId(agentImage.getAgentId());
        if(agentImg!=null) {
            agentImg.setImgUrl(agentImage.getImgUrl());
            iAgentImageDAO.update(agentImg);
            return iAgentImageDAO.get(agentImg.getId());
        }
        return iAgentImageDAO.get(iAgentImageDAO.save(agentImage));
    }

    /**
     * 获取代理客服图片
     */
    @Override
    public AgentImage getAgentImage(long agentId) {
        AgentImage agentImg=   iAgentImageDAO.getAgentImageByAgentId(agentId);
        if(agentImg!=null)
            return agentImg;
        return null;
    }

    /**
     * 查询所有下级代理明细
     * @return
     */
    @Override
    public JSONObject getLowerAgent(long id,  Timestamp startTime,Timestamp endTime,int startPage) {
        List list;
        Admin ad=iAdminDAO.getAdminById(id);
        if(ad==null){
            return null;
        }
        try {
            list= iAdminDAO.getLowerAgent(ad.getLeftNum(),ad.getRightNum(),startPage);
        }catch (NullPointerException e){
            return null;
        }



        JSONArray jsonArray=new JSONArray();
        for (Admin admin:(List<Admin>) list.get(0)) {
            User user=iUserFootDAO.selectUserByUid(admin.getUid());//获得代理的用户信息
            if(user==null)
                continue;
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",admin.getId());
            jsonObject.put("uid",admin.getUid());
            jsonObject.put("nickName",user.getNickName());
            jsonObject.put("headImgUrl",user.getHeadImgUrl());
            double balance=iUserFootDAO.getAgentRechargeMoney(user.getProxyId(), startTime,endTime);//当天充值金额总数
            System.out.println("balance........."+balance);
            jsonObject.put("balance",balance);
            double comm=iUserFootDAO.getAgentCommMoney(user.getProxyId(),startTime,endTime);//当天金额支出
            jsonObject.put("commission",comm);
            //balance-iUserFootDAO.getAgentCashComm(user.getProxyId(),findTime)
            jsonObject.put("money",balance-comm);//实际收入      充值的金额-返佣金金额
            jsonArray.add(jsonObject);
        }

     JSONObject jsonObject=new JSONObject();
        jsonObject.put("totalPage",list.get(1));
        jsonObject.put("inPage",list.get(2));
        jsonObject.put("value",jsonArray);
        return jsonObject;
    }

    /**
     * 根据id查询单个代理信息
     * @param id
     * @param startTime
     * @param endTime
     * @param leftNum
     * @param rightNum
     * @return
     */
    @Override
    public JSONObject getAgentById(long id, Timestamp startTime,Timestamp endTime,int leftNum,int rightNum) {

        Admin admin=  iAdminDAO.getAgentAdminById(id,leftNum,rightNum);
        if(admin==null)
            return null;
        User user=iUserFootDAO.selectUserByUid(admin.getUid());//获得代理的用户信息
        //if(user==null)

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",id);
        jsonObject.put("uid",admin.getUid());
        jsonObject.put("nickName",user.getNickName());
        jsonObject.put("headImgUrl",user.getHeadImgUrl());
        double balance=iUserFootDAO.getAgentRechargeMoney(user.getProxyId(), startTime,endTime);//当天充值金额总数
        System.out.println("balance........."+balance);
        jsonObject.put("balance",balance);
        double comm=iUserFootDAO.getAgentCommMoney(user.getProxyId(),startTime,endTime);//当天金额支出
        jsonObject.put("commission",comm);
        //balance-iUserFootDAO.getAgentCashComm(user.getProxyId(),findTime)
        jsonObject.put("money",balance-comm);//实际收入      充值的金额-返佣金金额
//        jsonObject.put("balance",iUserFootDAO.getAgentRechargeMoney(user.getProxyId(), startTime,endTime));//当天充值金额总数
//        double comm=iUserFootDAO.getAgentCommMoney(user.getProxyId(),startTime,endTime);//当天金额支出
//        jsonObject.put("commission",comm);
//        jsonObject.put("money",comm-iUserFootDAO.getAgentCashComm(user.getProxyId(),startTime,endTime));//实际收入      支出的金额-提现的金额

        return jsonObject;
    }

    /**
     * 取消授权代理
     * @param id
     * @return
     */
    @Override
    public List<Object[]> deleteAgent(long id) {
       Admin admin= iAdminDAO.getAdminById(id);

       iAdminDAO.remove(admin);//删除代理的信息
        User user=iUserFootDAO.selectUserByUid(admin.getUid());
        long proxyId=1;

        if(user.getParentId()!=1){
            if(  user.getParentId()==iUserFootDAO.selectUserByUid(user.getParentId()).getProxyId())
                proxyId=user.getParentId();
        }

         int result=  iUserFootDAO.updateUserProxyId(admin.getUid(),proxyId);
          //  JSONObject jsonObject=new JSONObject();
            if(result>0){
               List<Object[]> list= iUserFootDAO.getProxyUserId(proxyId);
               if(list!=null)
                   return list;
            }
            return null;

    }

    @Override
    public Admin getAdminById(long id) {
        return iAdminDAO.getAdminById(id);
    }

    @Override
    public Boolean checkAgentName(String name) {
        Admin user= iAdminDAO.getUserByName(name);
        if(user!=null)
            return true;
        return  false;
    }

    @Override
    public Boolean checkAgentUser(long uid) {
        Admin user= iAdminDAO.getAdminByUId(uid);
        if(user!=null)
            return true;
        return  false;
    }

    @Override
    public boolean updateAgentRightNum(int leftNum,long id) {

        return iAdminDAO.updateAgentRightNum(leftNum,id);
    }

    @Override
    public boolean updateAgentLeftNum(int rightNum,long id) {

        return iAdminDAO.updateAgentLeftNum(rightNum,id);
    }


}
